<template>
  <el-form ref="deptForm" :rules="rules" :model="form" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input
        v-model="form.name"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input
        v-model="form.code"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
        <el-option
          v-for="item in list"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input
        v-model="form.introduce"
        style="width: 80%"
        placeholder="1-300个字符"
        type="textarea"
        :rows="3"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {
  getDepartmentSimple,
  addDepartment,
  getDepartmentDetail,
  editDepartment
} from '@/api/departments'
export default {
  name: 'VueHr8802DeptDialog',
  props: {
    orangeList: {
      type: Array,
      required: true
    },
    id: {
      type: String,
      required: true
    },
    iEdit: {
      type: Boolean,
      required: true
    }
  },

  data() {
    const existCodeValid = (rules, value, callback) => {
      let existCode = this.orangeList.map((item) => item.code)
      if (this.iEdit) {
        existCode = this.orangeList
          .filter((item) => item.id !== this.id)
          .map((item) => item.code)
      }
      console.log(existCode)
      if (existCode.includes(value)) {
        callback(new Error('部门编码已存在'))
      } else {
        callback()
      }
    }
    const existNameValid = (rules, value, callback) => {
      let existNameList = this.orangeList
        .filter((item) => item.pid === this.id)
        .map((item) => item.name)
      if (this.iEdit) {
        existNameList = this.orangeList
          .filter(
            (item) =>
              item.pid ===
              this.orangeList.find((item) => item.id === this.id).pid
          )
          .filter((item) => item.id !== this.id)
          .map((item) => item.name)
      }
      if (existNameList.includes(value)) {
        callback(new Error(`部门名称已存在`))
      } else {
        callback()
      }
    }
    return {
      // 表单收集的数据
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: this.id
      },
      // 保存的是部门负责人

      list: [],
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称为1-50位', trigger: 'blur' },
          { validator: existNameValid, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码为1-50位', trigger: 'blur' },
          { validator: existCodeValid, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择部门负责人', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 50, message: '部门介绍为1-50位', trigger: 'blur' }
        ]
      }
    }
  },

  mounted() {},
  created() {
    this.loadDepartmentSimple()
    if (this.iEdit) {
      this.loadDepartmentDetail()
    }
  },

  methods: {
    // 清空表单
    resetForm() {
      this.$refs.deptForm.resetFields()
    },
    // 新增子部门
    async doAdd() {
      try {
        const res = await addDepartment(this.form)
        console.log(res)
        this.$message.success(res.message)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    async doEdit() {
      try {
        const res = await editDepartment(this.form)
        console.log(res)
        this.$message.success(res.message)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取部门详情
    async loadDepartmentDetail() {
      try {
        const res = await getDepartmentDetail(this.id)
        console.log(res)
        this.form = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 获取部门的负责人
    async loadDepartmentSimple() {
      try {
        const res = await getDepartmentSimple()
        console.log(res)
        this.$message.success(res.message)
        this.list = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 确定按钮
    hSubmit() {
      this.$refs.deptForm.validate((valid) => {
        if (!valid) return
        this.iEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 取消按钮
    hCancel() {
      this.$emit('cancel')
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
